//
//  SINAStuActivityAddImpressionViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2018/1/23.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "SINAStuMyAlertSignNameController.h"
#import "SINAUiviewClickLiserner.h"
#import "SINAPhotoPopView.h"
#import "SINAHttpRequestUtils.h"
#import "SINACircularBeadUtils.h"

@interface SINAStuMyAlertSignNameController ()<UITextViewDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UITextView *inputDesTextView;
//字数限制显示
@property (weak, nonatomic) IBOutlet UILabel *textNumberLabel;

@property (weak, nonatomic) IBOutlet UIView *mainContentView;



@property(nonatomic) NSInteger selectImageNumber;

//选择图片的宽度
@property(nonatomic) NSInteger imageWidth;
@end

@implementation SINAStuMyAlertSignNameController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"修改签名";
    UIView *rightView =[[UIView alloc]initWithFrame:CGRectMake(0,0,40,50)];
    UIButton *rightButton =[[UIButton alloc]initWithFrame:CGRectMake(0,0,40,45)];
    [rightButton addTarget:self action:@selector(saveDataFunction) forControlEvents:UIControlEventTouchUpInside];
    [rightView addSubview:rightButton];
    [rightButton setTitle:@"确定" forState:UIControlStateNormal];
    UIBarButtonItem *rightButtonView = [[UIBarButtonItem alloc]initWithCustomView:rightView ];
    self.navigationItem.rightBarButtonItem=rightButtonView;
    
    self.inputDesTextView.delegate=self;
    
    
    self.inputDesTextView.backgroundColor = [UIColor whiteColor];
    
    
    __weak typeof(self) weakSelf = self;
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.view andWithBlick:^(UIView *view) {
        [weakSelf.view endEditing:YES];
    }];
    
    
    [SINACircularBeadUtils makeBorder:self.inputDesTextView andWithBorderWidth:1 andBorderRadius:2 andBorderColor:[UIColor colorWithHexString:@"#eeeeee"]];
    
}

#pragma mark - UITextViewDelegate
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if(textView.text.length < 1){
        textView.text = @"请设置你的签名";
        textView.textColor = [UIColor colorWithHexString:@"#7F7F7F"];
        self.textNumberLabel.text=@"0/30";
    }else{
        self.textNumberLabel.text=[NSString stringWithFormat:@"%ld/30",(long)textView.text.length];
    }
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if([textView.text isEqualToString:@"请设置你的签名"]){
        textView.text=@"";
        self.textNumberLabel.text=@"0/30";
        textView.textColor=[UIColor colorWithHexString:@"#2e2e2e"];
    }
}

#pragma mark textField的字数限制
//在这个地方计算输入的字数
- (void)textViewDidChange:(UITextView *)textView
{
    NSInteger wordCount = textView.text.length;
    self.textNumberLabel.text = [NSString stringWithFormat:@"%ld/30",  (long)wordCount];
    [self wordLimit:textView];
}
#pragma mark 超过50字不能输入
-(BOOL)wordLimit:(UITextView *)text{
    if (text.text.length <= 30) {
        DLog(@"%ld",(long)text.text.length);
        self.inputDesTextView.editable = YES;
    }
    else{
        if (self.inputDesTextView.text.length>30) {
         
        self.inputDesTextView.text=[self.inputDesTextView.text  substringWithRange:NSMakeRange(0, 30)];
            self.textNumberLabel.text=@"30/30";
        }
        self.inputDesTextView.editable = YES;
    }
    return nil;
}



//提交保存数据
-(void)saveDataFunction{
    
    [self.view endEditing:YES];
    
    NSString *userSign = self.inputDesTextView.text;
    
    if (userSign==nil||userSign.length<1) {
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"请输入签名" sureBtn:@"确定" cancleBtn:nil];
        [alertView showXLAlertView];
        return;
    }
    
    SINACommonUserHelper *helper =[SINACommonUserHelper share];
    SINAUserModel *userModel = [helper getUserModel];
    
    if (userModel!=nil&&userModel.userUid!=nil&&![userModel.userUid isEqualToString:@""]) {
    
        [self startLoading];
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
        [dict setObject:userModel.userUid forKey:@"uuid"];
        [dict setObject:userSign forKey:@"sign"];
        NSMutableDictionary *dict2 = [[NSMutableDictionary alloc]init];
        [dict2 setObject:[SINAHttpRequestUtils encodeDicParams:dict] forKey:@"sj"];
        WeakSelf;
        [CXAFNHttpRequestUtils httpPostRequest:[NSString stringWithFormat:@"%@%@",baseUrl,userAlertInformationUrl] withParamters:dict2 success:^(id responseData) {
            NSString *code = responseData[@"code"];
            
            [weakSelf stopLoading];
            
            if ([code isEqualToString:@"1000"]) {
//                self.loadingView.text=@"修改成功";
                
                [YPXToastView showToastViewWithText:@"修改成功" andDuration:2 andCorner:5 andParentView:weakSelf.view];
                
                SINACommonUserHelper *helper =[SINACommonUserHelper share];
                SINAUserModel *userModel = [helper getUserModel];
                userModel.sign = userSign;
                [helper saveUserModel:userModel];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                   
                });
            }else{
                NSString *message =responseData[@"message"];
                WISAlertView *alertView =  [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定" cancleBtn:nil];
                [alertView showXLAlertView];
            }
            
        } failure:^(NSError *error, NSInteger errCode) {
            DLog(@"加载 失败  %@ ",error.debugDescription);
            [weakSelf stopLoading];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"验证失败" sureBtn:@"确定" cancleBtn:nil];
            [alertView showXLAlertView];
        }];
    }else{
        
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"用户信息异常请重新进入" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
